<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clers-search-histories')">全部删除</span>
        <!-- 注意我是想清空数组的内容，而非要替换数组，所以最优对的方式应该是使用splice删除全部内容 -->
        <!-- <span @click="searchHistories.splice(0)">全部删除</span> -->
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else @click="isDeleteShow = true" name="delete-o" />
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="onSearchItemClick(item.index)"
    >
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    /*
      如果是普通数据（数字，字符串，布尔值）绝对不能修改
      即使改了也不会传导给父组件
      如果是引用类型数据
      */
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    onSearchItemClick (item, index) {
      if (this.isDeleteShow) {
        /* 删除状态 */
        this.searchHistories.splice(index, 1)
      } else {
        /* 非删除状态 */
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style>
</style>
